<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";

//定义变量控制弹窗是否出现
const dialogVisible = ref(false);
//定义变量用来保存弹窗的标题
const dialogTitle = ref();
//用户搜索
const searchUser = ref({name:'',type:'',status:''});
const userArr = ref([]);
//加载
const loadUser = ()=>{
  let data = qs.stringify(searchUser.value)

  axios.get(BASE_URL+"/v1/user/select?"+data).then((response)=>{
    if (response.data.code==2000){
      userArr.value = response.data.data;
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
onMounted(()=>{
  loadUser();
})
//搜索重置
const resetUser = ()=>{
  searchUser.value = {}
  loadUser()
}
//定义对象用来保存表单中的员工数据
const saveUserForm = ref({
  name:'',
  enable:'',
  phone:'',
  type:'',
  valid:'',
  status:''
});
//编辑用户
const editUser = (id)=>{
  dialogVisible.value = true;
  dialogTitle.value = '编辑员工';
  axios.get(BASE_URL+"/v1/user/select?id="+id).then((response)=>{
    if (response.data.code==2000){
      saveUserForm.value = response.data.data[0];
    }else {
      ElMessage.error(response.data.msg);
    }
  })

}
const saveUser = ()=>{
  let date = qs.stringify(saveUserForm.value)
  axios.post(BASE_URL+'/v1/user/save',date).then((response)=>{
    if(response.data.code == 2000){
      ElMessage.success('保存成功!');
      //保存成功后重新加载所有用户数据
      loadUser();
      //关闭弹窗
      dialogVisible.value = false;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//处理弹窗关闭的方法
const handleClose = ()=>{
  if(confirm("确定要关闭弹窗吗?")){
    //关闭弹窗且清空已输入的数据
    dialogVisible.value = false;

  }
}
//删除用户
const deleteUser = (id)=>{
  if (confirm("确定要删除该用户吗?")){
    axios.post(BASE_URL+"/v1/user/delete/"+id).then((response)=>{
      if (response.data.code==2000){
        ElMessage.success('删除成功!')
        loadUser()
      }else{
        ElMessage.error(response.data.msg);
      }
    })
  }
}
</script>

<template>
  <!-- 顶部条 -->
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">用户详情</span>
  </div>

  <!--用户详情搜索卡片-->
  <el-card style="margin: 20px;">
    <el-form :inline="true">
      <el-form-item label="用户名:" style="width: 35vh;">
        <el-input placeholder="请输入用户名" v-model="searchUser.name" @keydown.enter="loadUser"></el-input>
      </el-form-item>
      <el-form-item label="用户状态" style="width: 290px;">
        <el-select placeholder="请选择用户状态" v-model="searchUser.status">
          <el-option label="活跃" value="活跃"></el-option>
          <el-option label="禁用" value="禁用"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" style="width: 290px;">
        <el-select placeholder="请选择用户类型" v-model="searchUser.type">
          <el-option label="普通" value="普通"></el-option>
          <el-option label="VIP" value="VIP"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="resetUser">重置</el-button>
        <el-button type="primary" @click="loadUser">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!--用户详情数据表格卡片-->
  <el-card style="margin: 20px;">
    <el-table style="width: 100%" :data="userArr">
      <el-table-column label="编号" type="index" width="80" align="center"/>
      <el-table-column label="用户名" prop="name" align="center"/>
      <el-table-column label="是否可用" prop="enable" align="center"/>
      <el-table-column label="电话" prop="phone" align="center"/>
      <el-table-column label="类型" prop="type" align="center"/>
      <el-table-column label="有效期"  prop="valid" align="center"/>
      <el-table-column label="用户状态" prop="status" align="center"/>
      <el-table-column label="创建时间" prop="createTime" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="editUser(scope.row.id)">编辑</el-button>
          <el-button link type="primary" @click="deleteUser(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
<!-- 编辑用户卡片 -->
  <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      style="width: 1000px;padding:40px;"
      :before-close="handleClose"
  >
    <el-form>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="用户名">
            <el-input v-model="saveUserForm.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否可用">
            <el-input v-model="saveUserForm.enable"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="电话">
            <el-input v-model="saveUserForm.phone"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型">
            <el-select placeholder="请选择用户类型" v-model="saveUserForm.type">
              <el-option label="普通" value="普通"></el-option>
              <el-option label="VIP" value="VIP"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="有效期">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="saveUserForm.valid"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态">
            <el-select placeholder="请选择用户状态" v-model="saveUserForm.status">
              <el-option label="活跃" value="活跃"></el-option>
              <el-option label="禁用" value="禁用"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveUser">保存</el-button>
    </template>
  </el-dialog>
</template>

<style scoped>

</style>